package ctr.affectation;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import metier.AffectEleve;
import metier.jdbc.affectation.AffectEleveJdbc;
import configuration.MessageAppli;
import vue.MenuControle;
import vue.affectation.JPanSuppAffectation;

public class GestSuppAffectation implements ActionListener,MouseListener {

	private JPanSuppAffectation jPanSuppAffectation;

	public GestSuppAffectation(JPanSuppAffectation jPanSuppAffectation) {
		this.jPanSuppAffectation = jPanSuppAffectation;
	}
	
	@Override
	public void actionPerformed(ActionEvent a) {
	System.out.println(a.getActionCommand());
	
	if(a.getActionCommand()=="Rechercher")
	{
		MenuControle.afficherPan(new JPanSuppAffectation(this.jPanSuppAffectation));		
	}
	}

	@Override
	public void mouseClicked(MouseEvent m) {
		Integer index=this.jPanSuppAffectation.getJPanJTable().getJtable().getSelectedRow();
		
		if(index!=null)
		{
			// Message de confirmation de suppression.
			int rep=JOptionPane.showConfirmDialog(jPanSuppAffectation, MessageAppli.MSG_AFFECTATION_CONFIRM_SUPP);
			
			if(rep==JOptionPane.OK_OPTION)
			{
				ArrayList<AffectEleve> listeAffectEleve = this.jPanSuppAffectation.getListeAffectEleve();
				int ligneSel = this.jPanSuppAffectation.getJPanJTable().getJtable().getSelectedRow();
				AffectEleve affectEleveSel = listeAffectEleve.get(ligneSel);
				AffectEleveJdbc.supprimer(jdbc.Connexion.getConnexion(), affectEleveSel);
				MenuControle.afficherPan(new JPanSuppAffectation(this.jPanSuppAffectation));
			}		
		}	
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		
		
	}
}